package com.middlegroup.subjectpay.web.api;

import com.middlegroup.common.model.NotifyRequest;
import com.middlegroup.common.model.ResponseEntity;
import com.middlegroup.subjectpay.entity.SubjectInBills;
import com.middlegroup.subjectpay.entity.SubjectOutBills;
import com.middlegroup.subjectpay.service.SubjectInBillsService;
import com.middlegroup.subjectpay.service.SubjectOutBillsService;
import com.middlegroup.subjectpay.service.SubjectPayNotifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * @author SHUQWEI
 * @Pagke Name   com.middlegroup.subjectpay.web.api
 * @Description 手动通知支付中台
 * @create 2019-01-31 13:34
 **/
@Controller
@Slf4j
public class NotifyPayController {

    @Resource
    private SubjectInBillsService subjectInBillsService;

    @Resource
    private SubjectOutBillsService subjectOutBillsService;

    @Resource
    private SubjectPayNotifyUtil subjectPayNotifyUtil;

    /**
     * 通知支付中
     *
     * @param request
     * @param notifyType 1：代付 2：代扣 (后续可以继续增加)
     * @return
     */
    @RequestMapping(value = "notifyPay")
    public ResponseEntity notifyPay(NotifyRequest request, String notifyType) {
        if (StringUtils.isBlank(notifyType)) {
            return new ResponseEntity(ResponseEntity.STATUS_FAIL, null, "通知类型不能为空", null);
        }
        if (StringUtils.isBlank(request.getBillNo()) && StringUtils.isBlank(request.getMerchantBillNo())) {
            return new ResponseEntity(ResponseEntity.STATUS_FAIL, null, "商户订单号或平台订单号不能同时为空", null);
        }
        ResponseEntity ret = new ResponseEntity(ResponseEntity.STATUS_OK);
        switch (notifyType) {
            case "1":
                //查询代付记录
                SubjectOutBills findOutBills = subjectOutBillsService.getLastOneOutByBillNo(request.getBillNo(), request.getMerchantBillNo());
                if (findOutBills != null) {
                    request.setTransStatus(findOutBills.getStatus());
                    request.setTransTime(findOutBills.getAddTime());
                    request.setAmount(findOutBills.getAmount());
                    request.setMsg(statusToMsg(findOutBills.getStatus()));
                    subjectPayNotifyUtil.notifyPay(findOutBills.getSubjectCode(), "notify/df", request);
                }
                break;
            case "2":
                // 代扣查询
                SubjectInBills findInBills = subjectInBillsService.getLastOneInByBillNo(request.getBillNo(), request.getMerchantBillNo());
                if (findInBills != null) {
                    request.setTransStatus(findInBills.getStatus());
                    request.setTransTime(findInBills.getAddTime());
                    request.setAmount(findInBills.getAmount());
                    request.setMsg(statusToMsg(findInBills.getStatus()));
                    subjectPayNotifyUtil.notifyPay(findInBills.getSubjectCode(), "notify/deduct", request);
                }
                break;
            default:
                ret = new ResponseEntity(ResponseEntity.STATUS_FAIL, null, "通知类型错误", null);
        }
        return ret;
    }

    /**
     * 交易状态说明
     *
     * @param status
     * @return
     */
    private String statusToMsg(Integer status) {
        String msg = null;
        if (status == 0) { // 处理中
            msg = "交易处理中";
        } else if (status == 1) { // 成功
            msg = "交易成功";
        } else if (status == 2) { //失败
            msg = "交易失败";
        } else if (status == 3) { //超时
            msg = "交易超时";
        }
        return msg;
    }
}
